package de.baumann.browser.present

import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.Node
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.ICreateNodeView
import de.baumann.browser.model.NodeModel

class CreateNodePresenter : BasePresenter<ICreateNodeView>() {
    private val nodeModel = NodeModel()

    fun createNode() {
        this.nodeModel.createNode(view.getNodeName(), view.getNodeDesc())
                .subscribe(object : OdinObserver<Node>(view) {
                    override fun onSuccess(data: Node?) {
                        if (view != null && data != null) {
                            view.createSuccess(data.nodeId)
                        }
                    }
                })
    }

    fun updateNodeDesc(nodeId: String) {
        this.nodeModel.updateNodeDesc(nodeId, view.getNodeDesc())
                .subscribe(object : OdinObserver<Node>(view) {
                    override fun onSuccess(data: Node?) {
                        if (view != null) {
                            view.updateSuccess()
                        }
                    }
                })
    }
}